import ts from "typescript";

/**
 * Extract the string representation of a literal value
 */
export function extractLiteralValue(node: ts.Expression): string {
  if (ts.isStringLiteral(node)) {
    return node.text;
  }
  if (ts.isNumericLiteral(node)) {
    return node.text;
  }
  if (ts.isPrefixUnaryExpression(node)) {
    if (
      node.operator === ts.SyntaxKind.MinusToken &&
      ts.isNumericLiteral(node.operand)
    ) {
      return "-" + node.operand.text;
    }
  }
  if (node.kind === ts.SyntaxKind.TrueKeyword) {
    return "true";
  }
  if (node.kind === ts.SyntaxKind.FalseKeyword) {
    return "false";
  }
  return "";
}
